//
//  FavManager.m
//  MoeFM
//
//  Created by miyang on 14-11-12.
//  Copyright (c) 2014年 StraightJet. All rights reserved.
//

#import "FavManager.h"

@interface FavManager ()

@property (nonatomic,strong)NSUserDefaults *userDate;

@end

@implementation FavManager

+ (instancetype)sharedManager {
    static FavManager *favManager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        favManager = [[FavManager alloc] init];
    });
        
    return favManager;
}

- (void)checkStatus
{
    _userdetail = [[User alloc] init];
    _status = UserStatusLogOut;
    _userDate = [NSUserDefaults standardUserDefaults];
    _oauthToken = [_userDate objectForKey:@"oauthToken"];
    _oauthTokenSecret = [_userDate objectForKey:@"oauthTokenSecret"];
    if (_oauthToken != nil && ![_oauthToken isEqualToString:@""]) {
        
        NSURLRequest *request = [OAuth1Controller preparedRequestForPath:USER_DETAIL
                                                              parameters:nil
                                                              HTTPmethod:@"GET"
                                                              oauthToken:self.oauthToken
                                                             oauthSecret:self.oauthTokenSecret];
        
        AFHTTPRequestOperation *op = [[AFHTTPRequestOperation alloc] initWithRequest:request];
        op.responseSerializer = [AFJSONResponseSerializer serializer];
        [op setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
            NSLog(@"success");
            _status = UserStatusLogIn;
            NSDictionary *dic = [responseObject objectForKey:@"response"];
            dic = [dic objectForKey:@"user"];
            _userdetail.uid = [dic objectForKey:@"uid"];
            _userdetail.user_nickname = [dic objectForKey:@"user_nickname"];
            _userdetail.user_avatar = [dic objectForKey:@"user_avatar"];
            
            [self getFavsWiki];
            [self getFavsSub];
            
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            NSLog(@"%@",error);
            _status = UserStatusTokenError;
        }];
        
        
        [[NSOperationQueue mainQueue] addOperation:op];
    } else {
        _status = UserStatusLogOut;
    }
    
    
}

- (void)getFavsWiki
{
    if (_status == UserStatusLogIn) {
        //NSDictionary *parameters = [NSDictionary dictionaryWithObjectsAndKeys:@"radio,music",@"obj_type", nil];
        NSURLRequest *request = [OAuth1Controller preparedRequestForPath:FAV_WIKI_URL
                                                              parameters:nil
                                                              HTTPmethod:@"GET"
                                                              oauthToken:self.oauthToken
                                                             oauthSecret:self.oauthTokenSecret];
        AFHTTPRequestOperation *op = [[AFHTTPRequestOperation alloc] initWithRequest:request];
        op.responseSerializer = [AFJSONResponseSerializer serializer];
        [op setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
            NSDictionary *returndic = [responseObject valueForKey:@"response"];
            NSArray *favs = [returndic objectForKey:@"favs"];
            
            NSMutableArray *radioarr = [[NSMutableArray alloc] init];
            NSMutableArray *musicarr = [[NSMutableArray alloc] init];
            
            
            for (NSDictionary *favdic in favs) {
                Favs *favobj = [[Favs alloc] init];
                NSDictionary *obj = [favdic objectForKey:@"obj"];
                favobj.wiki_name = [obj objectForKey:@"wiki_name"];
                favobj.wiki_title = [obj objectForKey:@"wiki_title"];
                favobj.wiki_meta = [obj objectForKey:@"wiki_meta"];
                favobj.wiki_cover = [obj objectForKey:@"wiki_cover"];
                favobj.wiki_id = [obj objectForKey:@"wiki_id"];
                if ([[favdic valueForKey:@"fav_obj_type"] isEqualToString:@"radio"]) {
                    [radioarr addObject:favobj];
                } else if ([[favdic valueForKey:@"fav_obj_type"] isEqualToString:@"music"]) {
                    [musicarr addObject:favobj];
                }
            }
            _radioarr = radioarr;
            _musicarr = musicarr;

            
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            NSLog(@"%@",error);
        }];
        
        
        [[NSOperationQueue mainQueue] addOperation:op];
    }
    
    
    
}

- (void)getFavsSub
{
    if (_status == UserStatusLogIn) {
        NSDictionary *parameters = [NSDictionary dictionaryWithObjectsAndKeys:@"song",@"obj_type",@"100",@"perpage", nil];
        NSURLRequest *request = [OAuth1Controller preparedRequestForPath:FAV_SUB_URL
                                                              parameters:parameters
                                                              HTTPmethod:@"GET"
                                                              oauthToken:self.oauthToken
                                                             oauthSecret:self.oauthTokenSecret];
        AFHTTPRequestOperation *op = [[AFHTTPRequestOperation alloc] initWithRequest:request];
        op.responseSerializer = [AFJSONResponseSerializer serializer];
        [op setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
            NSDictionary *returndic = [responseObject valueForKey:@"response"];
            NSArray *favs = [returndic objectForKey:@"favs"];
            
            NSMutableArray *songarr = [[NSMutableArray alloc] init];
            for (NSDictionary *subDic in favs) {
                NSDictionary *obj = [subDic objectForKey:@"obj"];
                Sub *subItem = [[Sub alloc] init];
                subItem.sub_id = [obj objectForKey:@"sub_id"];
                subItem.sub_parent_wiki = [obj objectForKey:@"sub_parent_wiki"];
                subItem.sub_title = [obj objectForKey:@"sub_title"];
                subItem.sub_order = [obj objectForKey:@"sub_order"];
                subItem.sub_meta = [obj objectForKey:@"sub_meta"];
                subItem.wiki_dic = [obj objectForKey:@"wiki"];
                [songarr addObject:subItem];
            }
            _songarr = songarr;
            
            
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            NSLog(@"%@",error);
        }];
        
        
        [[NSOperationQueue mainQueue] addOperation:op];
    }
}

- (void)favsAddWithType:(NSString *)type andWikiID:(NSInteger)wikiID
{
    NSString *wikiIDStr = [NSString stringWithFormat:@"%d",wikiID];
    NSDictionary *parameters = [NSDictionary dictionaryWithObjectsAndKeys:type,@"fav_obj_type",wikiIDStr,@"fav_obj_id",@"1",@"fav_type",@"1",@"save_status", nil];
    NSURLRequest *request = [OAuth1Controller preparedRequestForPath:ADD_FAV
                                                          parameters:parameters
                                                          HTTPmethod:@"GET"
                                                          oauthToken:self.oauthToken
                                                         oauthSecret:self.oauthTokenSecret];
    AFHTTPRequestOperation *op = [[AFHTTPRequestOperation alloc] initWithRequest:request];
    op.responseSerializer = [AFJSONResponseSerializer serializer];
    [op setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSLog(@"add collection success");
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@",error);
    }];
    
    
    [[NSOperationQueue mainQueue] addOperation:op];
}

- (void)favsDelWithType:(NSString *)type andWikiID:(NSInteger)wikiID
{
    NSString *wikiIDStr = [NSString stringWithFormat:@"%d",wikiID];
    NSDictionary *parameters = [NSDictionary dictionaryWithObjectsAndKeys:type,@"fav_obj_type",wikiIDStr,@"fav_obj_id", nil];
    NSURLRequest *request = [OAuth1Controller preparedRequestForPath:DEL_FAV
                                                          parameters:parameters
                                                          HTTPmethod:@"GET"
                                                          oauthToken:self.oauthToken
                                                         oauthSecret:self.oauthTokenSecret];
    AFHTTPRequestOperation *op = [[AFHTTPRequestOperation alloc] initWithRequest:request];
    op.responseSerializer = [AFJSONResponseSerializer serializer];
    [op setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSLog(@"del collection success");
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@",error);
    }];
    
    
    [[NSOperationQueue mainQueue] addOperation:op];
}


@end
